package com.xhdx.ati.adau.controller.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xhdx.ati.adau.controller.service.flow.CwbxService;
import com.xhdx.ati.adau.entity.ActivityEntity;
import com.xhdx.ati.adau.entity.TaskEntity;
import com.xhdx.ati.adau.util.BaseRespone;
import com.xhdx.ati.adau.util.StringUtils;

@RestController
@RequestMapping("/myactivi/cwbx")
public class CwbxController {
	@Autowired
	private CwbxService cwbxService;

	/**职员提交申请
	 * 
	 * @param taskId 发起的任务id
	 * @param processDefinitionKey  流程定义key
	 * @param name 职员姓名
	 * @param money 报销金额
	 */
	@RequestMapping(value = "/completeEmployeeTask")
	public BaseRespone<?> completeEmployeeTask(String taskId, String processDefinitionKey, 
			@RequestParam(required = true) String name, @RequestParam(required = true) Float money) {
		if(StringUtils.isEmpty(taskId)){
			if(StringUtils.isEmpty(processDefinitionKey)){
				throw new NullPointerException("{taskId}为空的同时{processDefinitionKey}参数都不能为空");
			}
		}
		cwbxService.completeEmployeeTask(taskId, processDefinitionKey, name, money);
		return BaseRespone.SUCCESS.msg("职员提交申请成功");

	}

	/**领导审批
	 * 
	 * @param taskId 任务id,
	 * @param result 用0和1表示,0表示未通过,1表示通过
	 */
	@RequestMapping(value = "/completeLeaderTask")
	public BaseRespone<?> completeLeaderTask(String taskId, Integer result) {
		cwbxService.completeLeaderTask(taskId, result);
		return BaseRespone.SUCCESS.msg("领导审批成功");

	}

	/**经理审批
	 * 
	 * @param taskId 任务id
	 */
	@RequestMapping(value = "/completeMangerTask")
	public BaseRespone<?> completeMangerTask(String taskId, Integer result) {
		cwbxService.completeMangerTask(taskId, result);
		return BaseRespone.SUCCESS.msg("经理审批成功");

	}
	/**
	 * 查询所有正在运行任务(查询某人自己提交的任务/查询领导任务/查询经理任务)
	 * @param processDefinitionKey 流程定义key
	 * @param taskDefinitionKey 任务定义key
	 * @param applyName 申请人
	 */
	@RequestMapping(value = "/queryTasks")
	public BaseRespone<List<TaskEntity>>  queryTasks(String processDefinitionKey, String taskDefinitionKey, String applyName) {

		List<TaskEntity> taskList = cwbxService.queryTasks(processDefinitionKey, taskDefinitionKey, applyName);
		return BaseRespone.SUCCESS.content(taskList); 
	}
	
	/**
     * 历史活动查询
     * 
     */
	@RequestMapping(value = "/historyActInstanceList")
    public BaseRespone<List<ActivityEntity>> historyActInstanceList(String processInstanceId){
    	List<ActivityEntity> taskList = cwbxService.historyActInstanceList(processInstanceId);
		return BaseRespone.SUCCESS.content(taskList);
    }
    /**
     * 历史任务查询
     */
	@RequestMapping(value = "/historyTaskList")
    public BaseRespone<List<TaskEntity>> historyTaskList(String processInstanceId){
		List<TaskEntity> taskList = cwbxService.historyTaskList(processInstanceId);
		return BaseRespone.SUCCESS.content(taskList);
    }
    /**
     * 查询流程状态（正在执行 or 已经执行结束）
     */
	@RequestMapping(value = "/processState")
    public BaseRespone<String> processState(String processInstanceId){
		String info = cwbxService.processState(processInstanceId);
		return BaseRespone.SUCCESS.msg(info);
    }
}
